package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.FaultSupport;
import com.iot.ic.service.FaultSupportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(description = "故障支援信息相关接口")
@RestController
public class FaultSupportController extends BaseController {
	
	@Autowired
	private FaultSupportService faultSupportService;


    /**
	 * 添加故障支援信息
	 * @param userId
	 * @param faultSupport
	 * @return
     */
	@ApiOperation(value="添加故障支援信息", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/faultSupport", method=RequestMethod.POST)
	public Response addFaultSupport(
			@PathVariable("userId") int userId,
		    @Valid@RequestBody FaultSupport faultSupport ){
		Response response = new Response();
		int result = faultSupportService.addFaultSupportInfo(faultSupport);
		if (result == 1) {
            response.setInfos(ReturnCode.No_AddFaultSupportOK);
        } else {
            response.setInfos(ReturnCode.Err_AddFaultSupporFailor);
        }
		return response;
	}

	@ApiOperation(value="获取故障支援记录列表", notes="")
	@GetMapping(value = "/users/{userId}/faultSupports")
	public Response getFaultSupportList(@PathVariable("userId") int userId,
			@RequestParam(value = "faultSupportState",required = false,defaultValue = "-1")Integer faultSupportState){
		Response response=new Response();
		List<Map<String,Object>>result=faultSupportService.getFaultSupport(faultSupportState);
		response.setResult(result);
		return response;
	}

	@ApiOperation(value="根据参赛队伍获取故障支援记录列表", notes="")
	@GetMapping(value = "/users/{userId}/teams/{teamId}/faultSupports")
	public Response getFaultSupportListByTeam(
			@PathVariable("userId") int userId,
			@PathVariable("teamId") int teamId,
			@RequestParam(value = "faultSupportState",required = false,defaultValue = "-1")Integer faultSupportState){
		Response response=new Response();
		FaultSupport faultSupport=new FaultSupport();
		faultSupport.setTeamId(teamId);
		faultSupport.setFaultSupportState(faultSupportState);
		List<Map<String,Object>>result=faultSupportService.getFaultSupportByTeam(faultSupport);
		response.setResult(result);
		return response;
	}
	
	
	@ApiOperation(value="处理故障支援记录", notes="")
	@PutMapping(value = "/users/{userId}/faultSupports/{faultSupportId}")
	public Response handleFaultSupport(
			@PathVariable("userId") int userId,
			@PathVariable("faultSupportId") int faultSupportId){
		Response response = new Response();
		int result = faultSupportService.handleFaultSupport(faultSupportId);
		if (result==1)
			response.setResult(result);
		else
			response.setInfos(ReturnCode.Err_RequestFailed);
		return response;
	}

	@ApiOperation(value="删除故障支援记录", notes="")
	@DeleteMapping("/users/{userId}/faultSupports/{faultSupportId}")
	public Response delFaultSupport(@PathVariable("userId") int userId,
									@PathVariable("faultSupportId") int faultSupportId){
		Response response = new Response();
		int result = faultSupportService.delFaultSuppoert(faultSupportId);
		if (result==1)
			response.setResult(result);
		else
			response.setInfos(ReturnCode.Err_RequestFailed);
		return response;
	}
	
	@ApiOperation(value="根据参赛队伍获取最近的故障支援记录", notes="")
	@GetMapping(value = "/users/{userId}/teams/{teamId}/faultSupport")
	public Response getLeatFaultSupportByTeam(
			@PathVariable("userId") int userId,
			@PathVariable("teamId") int teamId){
		Response response=new Response();
		FaultSupport result=faultSupportService.getFaultSuppoertByTeamId(teamId);
		response.setResult(result);
		return response;
	}
	
	
}
